package com.waitingresult.reflection.gettype;

import com.waitingresult.entity.User;

import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.ArrayList;

/**
 * @Author: WangShuai
 * @CreateTime: 2022-11-25  15:56
 * @Description: TODO
 * @Version: 1.0
 */
public class TypeGetMain {
    public static void main(String[] args) throws Exception{
        DecimalFormat decimalFormat = new DecimalFormat("0.000000");


        BigDecimal bigDecimal = new BigDecimal(1.2);


        //超过5位默认进行四舍五入
        String strVal = decimalFormat.format(bigDecimal);
        System.out.println(bigDecimal);
        System.out.println(strVal);


        BigDecimal bigDecimal1 = BigDecimal.valueOf(1.20000);


        bigDecimal1.setScale(3, RoundingMode.UP);
        System.err.println(bigDecimal1);
        System.err.println("=========");
        //不足5位补0
        String _strVal = decimalFormat.format(bigDecimal);
        System.out.println(_strVal);
        System.err.println(bigDecimal);

        System.err.println("===");
        ArrayList<User> list = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            User user = new User();
            user.setName("zhangsna");
            user.setPrice(BigDecimal.valueOf(2344.178924+i));
            list.add(user);
           // System.err.println(user);
        }
        System.err.println(list);


        String bigdecimalString = "java.math.BigDecimal";

        for (User user : list) {
            Class<? extends User> aClass = user.getClass();
            Field[] declaredFields = aClass.getDeclaredFields();
            for (Field declaredField : declaredFields) {
                String name = declaredField.getType().getName();
                if(bigdecimalString.equals(name)){
                    declaredField.setAccessible(true);
                    BigDecimal reflectionValue = (BigDecimal)declaredField.get(user);
                    declaredField.set(user,reflectionValue.setScale(3, RoundingMode.UP));
                    BigDecimal reflectionValue2 = (BigDecimal)declaredField.get(user);
                    System.err.println(reflectionValue +"   :   "+ reflectionValue2);
                }
            }
        }
        System.err.println(list);

    }
}
